package com.hntxk.cqzd.diacrisis.action;

import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisFile;
import com.hntxk.cqzd.diacrisis.service.DiacrisisFileService;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("cqzd/do/diacfile")
@Slf4j
public class DiacrisisFileController extends CqzdBaseController {

    private  final DiacrisisFileService diacrisisFileService;

    public DiacrisisFileController(@Qualifier("diacFileService") DiacrisisFileService diacrisisFileService) {
        super(diacrisisFileService, LogManager.getLogger(DiacrisisFileController.class));
        this.diacrisisFileService = diacrisisFileService;
    }

    /**
     * 根据文件code下载文件
     * @param fileCode
     * @return
     */
    @ApiImplicitParam(name = "fileCode", value = "文件Code",example = "abcde",required = true, dataTypeClass = String.class)
    @GetMapping(value = "getFileByCode/{fileCode}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseEntity<byte[]> getFile(@PathVariable("fileCode") String fileCode) {
        return diacrisisFileService.getFile(fileCode);
    }

    /**
     * 根据外键id获取与之相关的所有文件
     * @param fkId
     * @return
     */
    @GetMapping(value = "getFileByFkId/{fkId}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getFileFkId(@PathVariable("fkId") String fkId) {
        Long diacrisisId=Long.parseLong(fkId);
        List<TblDiacrisisFile> byFkId = diacrisisFileService.getByFkId(diacrisisId);
        return toJson(byFkId);
    }


}
